{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.match = {\n",
    "            '(': 0,\n",
    "            ')': 1,\n",
    "            '[': 2,\n",
    "            ']': 3,\n",
    "            '{': 4,\n",
    "            '}': 5\n",
    "        }\n",
    "    def isMatch(self, left, right) -> bool:\n",
    "        if self.match[left] + 1 == self.match[right]:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "    def isValid(self, s: str) -> bool:\n",
    "        if s == '':\n",
    "            return True\n",
    "        stack = []\n",
    "        for i in range(len(s)):\n",
    "            if len(stack) == 0:\n",
    "                stack.append(s[i])\n",
    "            else:\n",
    "                if self.isMatch(stack[-1], s[i]):\n",
    "                    stack.pop()\n",
    "                else:\n",
    "                    stack.append(s[i])\n",
    "\n",
    "        if len(stack) == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Solution()\n",
    "s.isValid('()[]')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3",
   "language": "python",
   "name": "py3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
